﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BASE;

public partial class Admin_dsykien : BasePage
{
    public int cout = 1;
    string pageSize = "20";
    protected void Page_Load(object sender, EventArgs e)
    {

        //check quyen vao trang

        if (Utils.getIntSession("userid") == 0)
        {
            Response.Redirect("/admin/login.aspx");
        }
        busMenu busM = new busMenu();
        if (!busM.checkQuyenVaoTrang(Utils.getIntSession("userid"), Constants.ADMINY_DSYKIEN))
        {
            Session["LVT"] = Constants.LVT;
            Response.Redirect("/admin/");
        }
        //set session forcus trang
        Session["trang"] = Constants.ADMINYKIEN;

        gr_tin.RowCommand += new GridViewCommandEventHandler(gr_tin_RowCommand);
        if (!IsPostBack)
        {

            this.GetCustomersPageWise(1);


        }
    }
    void bindata(int pageIndex, string pageSize)
    {

        busYKien busYK = new busYKien();

        List<entYKien> lst = new List<entYKien>();
        lst = busYK.getAll2(pageIndex, pageSize);
        gr_tin.DataSource = lst;
        gr_tin.DataBind();
        int recordCount = 0;
        if (lst.Count > 0)
        {
            recordCount = int.Parse(lst[0].recordCount);
        }
        PopulatePager(recordCount, pageIndex);

    }
    void gr_tin_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            busYKien busY = new busYKien();
            busY.delete(int.Parse(e.CommandArgument.ToString()));

        }
        if (e.CommandName == "khoa")
        {

            Pa_hide.Visible = true;
            pa_mail.Visible = false;
            lb_masv.Text = e.CommandArgument.ToString();
        }
        if (e.CommandName == "mail")
        {
            Pa_hide.Visible = false;
            pa_mail.Visible = true;
            lb_masv_mail.Text = e.CommandArgument.ToString();
            entSinhVien entSV = new entSinhVien();
            busSinhVien busSV = new busSinhVien();
            entSV = busSV.getSinhVienByMaSV(e.CommandArgument.ToString());
            lb_mail.Text = entSV.Email;
        }
    }

    #region  ========phan trang=======
    protected void PageSize_Changed(object sender, EventArgs e)
    {
        this.GetCustomersPageWise(1);
    }

    private void GetCustomersPageWise(int pageIndex)
    {
        bindata(pageIndex, pageSize);
    }
    protected void Page_Changed(object sender, EventArgs e)
    {
        int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
        this.GetCustomersPageWise(pageIndex);
    }

    private void PopulatePager(int recordCount, int currentPage)
    {
        double dblPageCount = (double)((decimal)recordCount / decimal.Parse(pageSize));
        int pageCount = (int)Math.Ceiling(dblPageCount);
        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            pages.Add(new ListItem("First", "1", currentPage > 1));
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
            pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
        }
        rptPager.DataSource = pages;
        rptPager.DataBind();
    }

    #endregion
    protected void btn_ban_Click(object sender, EventArgs e)
    {
        entKhoaNick entKN = new entKhoaNick();
        entKN.LyDo = txt_lydo.Text;
        entKN.MaSinhVien = lb_masv.Text;
        busKhoaNick busKN = new busKhoaNick();
        busKN.khoaNick(entKN);
        Pa_hide.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<WebControl> lstW = new List<WebControl>();
        lstW.Add(txt_chude);
        lstW.Add(txt_noidung_mail);
        String check = Utils.checkDataType(lstW);
        if (check != "")
        {
            MessenBox(1, check);
            return;
        }
        List<String> lstMail = new List<string>();
        lstMail.Add(lb_mail.Text);
        Utils.SendMail(lstMail, txt_chude.Text, txt_noidung_mail.Text);
        MessenBox(1, "Gửi thành công");
        pa_mail.Visible = false;
    }
}